﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace RapChieuPhim.GUI
{
    public partial class QLLichChieu : UserControl
    {
        private int _MaRap = Modules.Login.user.NhanVien.Rap.MaRap;
        int _landau = 0;

        public QLLichChieu()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            lookPhongChieu.Properties.DataSource = BUS.PhongChieuBUS.GetAllbyMaRap(_MaRap);
            if (_landau==0)
                lookPhongChieu.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("MaPhong", 100, "Phòng"));
            lookPhongChieu.Properties.DisplayMember = "MaPhong";
            lookPhongChieu.Properties.ValueMember = "MaPhong";
            lookPhongChieu.Properties.PopupFormMinSize = new Size(100, 100);
            lookPhongChieu.ItemIndex = 0;

            lookPhim.Properties.DataSource = BUS.PhimBUS.GetByMaRap(_MaRap);
            if (_landau==0)
            {
                lookPhim.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("MaPhim", 30, "Mã"));
                lookPhim.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenPhim", 180, "Mã phim"));
                lookPhim.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("NamSanXuat", 60, "Năm SX"));
            }
            lookPhim.Properties.DisplayMember = "TenPhim";
            lookPhim.Properties.ValueMember = "MaPhim";
            lookPhim.Properties.PopupFormMinSize = new Size(280, 100);
            lookPhim.ItemIndex = 0;
            dateEdit1.EditValue = DateTime.Now;

            gridControlMain.DataSource = BUS.CTPhongBUS.GetAllbyMaRap(_MaRap);
            _landau++;
        }

        private void QLLichChieu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0 || gridView1.FocusedRowHandle >= gridView1.RowCount)
                return;
            int _MaPhim = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colMa).ToString());
            int _MaPhong = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colPhong).ToString());
            DateTime _ngay = DateTime.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colNgayChieu).ToString());
            DTO.CTPhongChieu _CTPhong = BUS.CTPhongBUS.GetByID(_MaRap, _MaPhong,_MaPhim, _ngay);
            BindingData(_CTPhong);
        }

        private void BindingData(DTO.CTPhongChieu _CTPhong)
        {
            lookPhim.EditValue = _CTPhong.Phim.MaPhim;
            lookPhongChieu.EditValue = _CTPhong.Phong.MaPhong;
            dateEdit1.EditValue = _CTPhong.NgayChieu;
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            int _MaPhim = int.Parse(lookPhim.EditValue.ToString());
            string _TenPhim = BUS.PhimBUS.GetByID(_MaPhim).TenPhim;
            int _MaPhong = int.Parse(lookPhongChieu.EditValue.ToString());
            DateTime _NgayChieu = DateTime.Parse(dateEdit1.EditValue.ToString());
            if (XtraMessageBox.Show("Thêm lịch chiếu cho phim   [" + _TenPhim + "]  vào ngày   [" + _NgayChieu.ToShortDateString() + "]   tại phòng   [" + _MaPhong + "]  ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataTable _dt = BUS.CTPhongBUS.CheckLichTrung(_MaPhong, _MaPhim, _NgayChieu, _MaRap);
                if (_dt.Rows.Count >0)
                    XtraMessageBox.Show("Lịch chiếu này có rồi. Không thêm được nữa", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                {
                    BUS.CTPhongBUS.Insert(_NgayChieu, _MaPhong, _MaPhim, _MaRap);
                    LoadData();
                    XtraMessageBox.Show("Thêm thành công lịch chiếu cho phim   [" + _TenPhim + "]   vào ngày   [" + _NgayChieu.ToShortDateString() + "]", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void navBarItem9_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            colPhong.GroupIndex = -1;
            colTen.GroupIndex = -1;
            colNgayChieu.GroupIndex = -1;
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            navBarItem9_LinkClicked(null, null);
            colTen.GroupIndex = 0;
        }

        private void navBarItem3_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            navBarItem9_LinkClicked(null, null);
            colNgayChieu.GroupIndex = 0;
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            navBarItem9_LinkClicked(null, null);
            colPhong.GroupIndex = 0;
        }

        private void navBarItem5_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            int _MaPhim = int.Parse(lookPhim.EditValue.ToString());
            string _TenPhim = BUS.PhimBUS.GetByID(_MaPhim).TenPhim;
            int _MaPhong = int.Parse(lookPhongChieu.EditValue.ToString());
            DateTime _NgayChieu = DateTime.Parse(dateEdit1.EditValue.ToString());
            if (XtraMessageBox.Show("Hủy lịch chiếu này  ?", null, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                BUS.CTPhongBUS.DeleteCuThe(_MaPhong, _MaPhim, _NgayChieu, _MaRap);
                LoadData();
                XtraMessageBox.Show("Thao tác hủy thành công", null, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            LoadData();
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            gridControlMain.ShowPrintPreview();
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            SaveFileDialog _dialog = new SaveFileDialog();
            _dialog.InitialDirectory = Application.StartupPath.ToString();
            _dialog.Title = "Xuất ra file pdf";
            _dialog.DefaultExt = "pdf";
            _dialog.Filter = "All files (*.*)|*.*";
            _dialog.FilterIndex = 0;
            _dialog.RestoreDirectory = true;
            if (_dialog.ShowDialog() == DialogResult.OK)
            {
                gridControlMain.ExportToPdf(_dialog.FileName.ToString());

                System.Diagnostics.Process _prc = new System.Diagnostics.Process();
                _prc.StartInfo.FileName = System.IO.Path.GetDirectoryName(_dialog.FileName);
                _prc.Start();
            }
        }

    }
}
